package com.th.supcom.pds.phamapply.client.widget.inpharmacyreturn;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Label;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.DateTimePropertyEditor;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.EditorGrid;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Element;
import com.th.supcom.pds.auditprescription.client.model.PdsPrescDetailCombineModel;
import com.th.supcom.pds.common.client.commom.SimpleMethod;
import com.th.supcom.pds.common.client.model.CodeConstants;
import com.th.supcom.pds.common.client.model.PdsSupplyDetailModel;
import com.th.supcom.pds.common.enums.BatchNumberManagerFlag;
import com.th.supcom.pds.common.utils.ClientUtils;
import com.th.supcom.pds.internal.client.widget.PhamBatchCombox;
import com.th.supcom.pds.phamapply.client.model.InpPhamApplyDetailModel;

/**
 * 退药信息面板
 * 
 * @author yzhang
 */
public class InPharmacyReturnGridPanel extends ContentPanel
{
    private Grid <InpPhamApplyDetailModel> m_grid;
    private ListStore <InpPhamApplyDetailModel> m_store;
    private PhamBatchCombox m_combox;

    /**
     * 构造方法
     * 
     * @param drugReturnAndRefundFields
     * @param drugReturnButtons
     */
    public InPharmacyReturnGridPanel ()
    {
        initUI ();
    }

    /**
     * 初始化界面
     */
    private void initUI ()
    {

        setHeaderVisible (false);
        setBorders (false);
        setLayout (new FitLayout ());
        m_combox = new PhamBatchCombox (60);
        m_combox.setEditable (false);
        m_store = new ListStore <InpPhamApplyDetailModel> ();
        m_grid = new EditorGrid <InpPhamApplyDetailModel> (m_store, createGridHeader ());
        m_grid.getView ().setEmptyText ("暂时没有您需要的数据...");
        m_grid.setStripeRows (true);
        m_grid.setColumnLines (true);
        m_grid.getView ().setAutoFill (true);
        m_grid.setBorders (false);
        m_grid.setColumnReordering (true);
        add (m_grid);
        ToolBar toolBar = createColorShow ();
        ContentPanel bottomContainer = new ContentPanel ();
        bottomContainer.setHeaderVisible (false);
        bottomContainer.add (toolBar);
        setBottomComponent (bottomContainer);
    }

    /**
     * 颜色提示
     * 
     * @return
     */
    private ToolBar createColorShow ()
    {
        ToolBar toolBar;
        toolBar = new ToolBar ();
        String txt = "<span class='grayTendGrade'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;可编辑</span>";
        Label lab = new Label (txt);
        lab.setStyleAttribute ("margin-left", "10px");
        toolBar.add (lab);
        return toolBar;
    }

    private ColumnModel createGridHeader ()
    {
        final List <ColumnConfig> columns = new ArrayList <ColumnConfig> ();

        ColumnConfig column = new ColumnConfig ();
        SimpleMethod.setColumnConfig (column, "patiName", "患者姓名", HorizontalAlignment.LEFT, 60, null);
        columns.add (column);

        column = new ColumnConfig ();
        SimpleMethod.setColumnConfig (column, "bedNo", "床号", HorizontalAlignment.LEFT, 40, null);
        columns.add (column);

        column = new ColumnConfig ();
        SimpleMethod.setColumnConfig (column, "nurseUnitName", "护理单元", HorizontalAlignment.LEFT, 100, null);
        columns.add (column);

        column = new ColumnConfig ();
        SimpleMethod.setColumnConfig (column, "phamName", "药品名称", HorizontalAlignment.LEFT, 120, null);
        columns.add (column);

        column = new ColumnConfig ();
        SimpleMethod.setColumnConfig (column, "phamSpec", "药品规格", HorizontalAlignment.LEFT, 60, null);
        columns.add (column);
        column = new ColumnConfig ();
        SimpleMethod.setColumnConfig (column, "applyAmount", "实退数量", HorizontalAlignment.LEFT, 40,
                                      ClientUtils.renderForNumber ("applyAmount"));
        columns.add (column);
        column = new ColumnConfig ();
        SimpleMethod.setColumnConfig (column, "applyUnitName", "单位", HorizontalAlignment.LEFT, 40, null);
        columns.add (column);

        column = new ColumnConfig ();
        SimpleMethod.setColumnConfig (column, "withdrawTypeName", "退药方式", HorizontalAlignment.LEFT, 60, null);
        columns.add (column);

        column = new ColumnConfig ();
        SimpleMethod.setColumnConfig (column, "phamBatchNo", "批次", HorizontalAlignment.LEFT, 50, renderForNumber ());
        column.setEditor (createCellEditor ());
        columns.add (column);

        column = new ColumnConfig ();
        SimpleMethod.setColumnConfig (column, "expiryDate", "有效期", HorizontalAlignment.LEFT, 70, renderForDate ());
        column.setEditor (cellEditorForDate ());
        column.setDateTimeFormat (DateTimeFormat.getFormat (CodeConstants.DATE_FORMAT_YYYY_MM_DD));
        columns.add (column);

        return new ColumnModel (columns);
    }

    private GridCellRenderer <InpPhamApplyDetailModel> renderForDate ()
    {
        return new GridCellRenderer <InpPhamApplyDetailModel> ()
        {
            @Override
            public Object render (final InpPhamApplyDetailModel model, final String property, final ColumnData config,
                                  final int rowIndex, final int colIndex,
                                  final ListStore <InpPhamApplyDetailModel> store,
                                  final Grid <InpPhamApplyDetailModel> grid)
            {
                if (BatchNumberManagerFlag.OnBatchManager.getEnumCode ().equals (model.getBatchNumberManagerFlag ()))
                {
                    config.cellAttr = "style='background-color:#E6E6F2; height:16px'";
                }
                return null;
            }
        };
    }

    private GridCellRenderer <InpPhamApplyDetailModel> renderForNumber ()
    {
        return new GridCellRenderer <InpPhamApplyDetailModel> ()
        {

            @Override
            public Object render (final InpPhamApplyDetailModel model, final String property, final ColumnData config,
                                  final int rowIndex, final int colIndex,
                                  final ListStore <InpPhamApplyDetailModel> store,
                                  final Grid <InpPhamApplyDetailModel> grid)
            {
                if (BatchNumberManagerFlag.OnBatchManager.getEnumCode ().equals (model.getBatchNumberManagerFlag ()))
                {
                    config.cellAttr = "style='background-color:#E6E6F2; height:16px'";
                }
                return null;
            }
        };
    }

    private CellEditor cellEditorForDate ()
    {
        DateField dateField = new DateField ();
        dateField.setPropertyEditor (new DateTimePropertyEditor (CodeConstants.DATE_FORMAT_YYYY_MM_DD));
        return new CellEditor (dateField)
        {
            @Override
            public Object postProcessValue (Object objvalue)
            {
                Object obj = objvalue;
                if (null == obj)
                {
                    return obj;
                }
                return obj;
            }

            @Override
            public void startEdit (final Element el, final Object value)
            {

                if (BatchNumberManagerFlag.UnBatchManager.getEnumCode ().equals (m_grid.getStore ().getAt (super.row)
                                                                                       .getBatchNumberManagerFlag ()))
                {
                    super.hide ();
                    return;
                }
                else
                {
                    super.startEdit (el, value);
                }
            }
        };
    }

    private CellEditor createCellEditor ()
    {
        m_combox.addSelectionChangedListener (new SelectionChangedListener <PdsSupplyDetailModel> ()
        {
            @Override
            public void selectionChanged (SelectionChangedEvent <PdsSupplyDetailModel> se)
            {
                if (se.getSelectedItem () == null)
                {
                    return;
                }
                m_grid.getSelectionModel ().getSelectedItem ().setExpiryDate (se.getSelectedItem ().getReceiptDate ());
            }
        });
        return new CellEditor (m_combox)
        {
            @Override
            public Object postProcessValue (final Object value)
            {
                if (null == value)
                {
                    return value;
                }
                return ((ModelData) value).get ("phamBatchNumber");
            }

            @Override
            public Object preProcessValue (final Object value)
            {
                if (null == value)
                {
                    return value;
                }

                final BaseModelData data = new BaseModelData ();
                data.set ("phamBatchNumber", value);
                return data;
            }

            @Override
            public void startEdit (final Element el, final Object value)
            {
                if (BatchNumberManagerFlag.UnBatchManager.getEnumCode ().equals (m_store.getAt (super.row)
                                                                                        .getBatchNumberManagerFlag ()))
                {
                    super.hide ();
                    return;
                }
                else
                {
                    final InpPhamApplyDetailModel detailInfoModel = m_store.getAt (super.row);
                    final PdsPrescDetailCombineModel combinemodel = new PdsPrescDetailCombineModel ();
                    combinemodel.setPerformedBy (detailInfoModel.getApplyDispensary ());
                    combinemodel.setPhamCode (detailInfoModel.getPhamStdCode ());
                    //m_combox.loadData (combinemodel);
                    super.startEdit (el, value);
                }
            }
        };
    }

    /**
     * 加载数据
     * 
     * @param models
     */
    public void refresh (List <InpPhamApplyDetailModel> models)
    {
        m_store.removeAll ();
        m_store.add (models);
    }

    public Grid <InpPhamApplyDetailModel> getGrid ()
    {
        return m_grid;
    }

    public ListStore <InpPhamApplyDetailModel> getStore ()
    {
        return m_store;
    }

    /**
     * 获取表格中的models
     * 
     * @return
     */
    public List <InpPhamApplyDetailModel> getModels ()
    {
        return m_store.getModels ();
    }

}
